/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package progin.java;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author Hallucinogen
 */
public class Room {
    public Room () {
        mMessages   = new CopyOnWriteArrayList<Message>();
        mUsers      = new CopyOnWriteArrayList<User>();
    }

    public int getParentCategoryID() {
        return mParentCategoryID;
    }

    public CopyOnWriteArrayList<User> getUsers() {
        return mUsers;
    }

    public User getUser(String username) {
        User user = null;
        for (int i = 0; i < mUsers.size(); ++i) {
            if (mUsers.get(i).getUsername().equalsIgnoreCase(username)) {
                user = mUsers.get(i);
                break;
            }
        }
        return user;
    }

    public CopyOnWriteArrayList<Message> getMessages() {
        return mMessages;
    }

    public String getRoomName(){
        return mRoomName;
    }

    public void setParentCategoryID(int id){
        mParentCategoryID = id;
    }

    public User getRoomOwner(){
        return RoomOwner;
    }

    public void setRoomOwner(User theUser) {
        RoomOwner = theUser;
    }

    public boolean isOwnerUser(String username) {
        if (RoomOwner.getUsername().equalsIgnoreCase(username)) { //kalau benar room ownernya ini, true
            return true;
        } else {

            return false;
        }
    }

    public boolean getpersistence(){
        return persistence;
    }

    public void setpersistence(boolean  thepersistence){
        persistence = thepersistence;
    }

    public void setRoomName(String name){
        mRoomName = name;
    }

    public void addBannedUsers(String user){
        BannedUsers.add(user);
    }

    public String getBannedUsers(String username){
        String user = "";
        for (int i = 0; i < BannedUsers.size(); ++i) {
            if (BannedUsers.get(i).equalsIgnoreCase(username)) {
                user = BannedUsers.get(i);
                break;
            }
        }

        return user; //kalau ga dapet keluarin default
    }

    public void delBannedUsers(String username) {
        BannedUsers.remove(username);
    }

    public boolean isBannedUser(String username) {
        for (int i = 0; i < BannedUsers.size(); ++i)
        {
            if (BannedUsers.get(i).equalsIgnoreCase(username)) {
                return true;
            }
        }
        return false;
    }

    public void addUser(User user) {
        boolean adapengguna = false;
        for(int i=0;i<mUsers.size();i++) //search semua user kalo dah ada, ga usah di add. Galo ga ada baru add
        {
            if(mUsers.get(i).getUsername().equalsIgnoreCase(user.getUsername())) //jika user sama, set true
            {adapengguna = true;}
        }

        if(adapengguna ==false) //kalo ga ada penggunanya
        {mUsers.add(user);}
        
    }
    
    public int FindIndexUser(String username) {
        for(int i=0;i<mUsers.size();i++)
        {
            if(mUsers.get(i).getUsername().equalsIgnoreCase(username)) //kao username sama, return
            {return i;}
        }
        return -1;
    }
    
    public void deleteUser(String username) {
        int indexuser = FindIndexUser(username);
        if (indexuser!=-1)
        {
                    mUsers.remove(indexuser);
        }

    }


    public void addMessage(Message message) {
        mMessages.add(message);
    }

    private int mParentCategoryID; //kalo 0 maka chatnya dengan user lain
    private String mRoomName;
    User RoomOwner = new User("", "", -1);
    boolean persistence = false;
    private CopyOnWriteArrayList<String> BannedUsers = new CopyOnWriteArrayList<String>();
    private CopyOnWriteArrayList<User> mUsers = new CopyOnWriteArrayList<User>();
    private CopyOnWriteArrayList<Message> mMessages = new CopyOnWriteArrayList<Message>();


    public void isidummy()
    {
       mParentCategoryID=1 ;
       mRoomName="cobcobah";
       User user1 = new User("Archie","127.0.0.1",1);
       User user2 = new User("Darwin","127.0.0.1",1);
       User user3 = new User("Gogo","127.0.0.1",1);
       mUsers.add(user1);
       mUsers.add(user2);
       mUsers.add(user3);
       mMessages.add(new Message(user1,"Hai"));
       mMessages.add(new Message(user3,"Hai juga say :*"));
       mMessages.add(new Message(user2,"Homo oy"));
       mMessages.add(new Message(user1,"Sakit si gogo -.-"));
       mMessages.add(new Message(user3,"Oh yeah"));

    }
}
